var webpack = require('webpack');
var path = require("path");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin");
// var commonsPlugin = new webpack.optimize.CommonsChunkPlugin({name:"common",filename:'js/common.js',chunks:['index'],minChunks: Infinity,});
module.exports = {
    //页面入口文件配置
    entry: {
        // index : './index',
        register : './src/register'
    },
    //入口文件输出配置
    output: {
        path: path.resolve(__dirname, "dist"),
        filename: 'js/[name].js',
        publicPath: "dist/"
    },
     //插件项
    plugins: [
        new ExtractTextPlugin("css/user.min.css",{allChunks: true}),
        new CommonsChunkPlugin("js/common.js",["register"])
    ],
    devServer:{
        historyApiFallback:true,
        hot:true,
        inline:true,
        progress:true,
        port:8090 //端口你可以自定义
    },
    module: {
        //加载器配置
        loaders: [
            { test: /\.vue$/, loader: 'vue'},
            // { test: /\.tpl$/, loader: 'html-loader'},
            { test: /\.less$/, loader: 'style!css!less'},
            { test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader','css-loader',"less-loader")},//.css 文件使用 style-loader 和 css-loader 来处理
            { test: /\.js$/, exclude:/node_modules|dist/,loader: 'babel'},
            // { test: /\.scss$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader!sass-loader')},//.scss 文件使用 style-loader、css-loader 和 sass-loader 来编译处理
            // { test: /\.less$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader!less-loader')},
            { test: /\.(png|jpe?g|gif)$/, loader: 'url-loader',query:{limit:8192,name:'images/[name].[ext]?[hash]'}}//图片文件使用 url-loader 来处理，小于8kb的直接转为base64  
        ]
    },
    vue: {
        loaders: {
            css: ExtractTextPlugin.extract("css"),
            // you can also include <style lang="less"> or other langauges
            // sass: ExtractTextPlugin.extract("css!sass"),
            less: ExtractTextPlugin.extract("css!less")
        }
    },
    babel: {
        presets: ['es2015'],
        plugins: ['transform-runtime']
    },
    //其它解决方案配置
    resolve: {
        //查找module的话从这里开始查找
        root: './', //绝对路径
        extensions: ['', '.js', '.json', '.scss', '.less'],//自动扩展文件后缀名，意味着我们require模块可以省略不写后缀名
        alias: {//模块别名定义，方便后续直接引用别名，无须多写长长的地址
            // zepto: 'src/lib/zepto.min.js',//后续直接 require('jquery') 即可
            // vue: 'src/lib/vue.min.js',
            // vueResource: 'src/lib/vue-resource.min.js',
            // vueRouter: 'src/lib/vue-router.min.js'
        }
    },
    externals: {//以下文件将不会打包，需要用script标签手动引入
        /*zepto: true,
        vue: true,
        vueResource: true,
        vueRouter: true,
        Swiper: true*/
    }
};
if (process.env.NODE_ENV === 'production') {
    module.exports.plugins.concat(//为concat合并，否则上面配置的plugin会丢失
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: '"production"'
            }
        }),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false
            }
        }),
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.optimize.CommonsChunkPlugin()
        // commonsPlugin
    )
} else {
    module.exports.devtool = '#source-map';
}